/*
 * Copyright 2021 <copyright holder> <email>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef VIEWER_H
#define VIEWER_H

#include <thread>
#include <pangolin/pangolin.h>
#include "common_include.h"
#include "frame.h"
#include "map.h"

/**
 * @todo write docs
 */
class Viewer
{
public:
    EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
    typedef std::shared_ptr<Viewer> Ptr;
    
    Viewer();
    
    void SetMap(Map::ptr map) {map_=map;}
    
    void Close();
    
    void AddCurrentFrame(Frame::Ptr current_frame);
    
    void UpdateMap();
     
private:
    void ThreatLoop();
    
    void DrawFrame(Frame::Ptr frame, const float* color);
    
    void DrawMapPoints();
    
    void FollowCurrentFrame(pangolin::OpenGlRenderState& vis_camera);
    
    cv::Mat PlotFrameImage();
    
    Frame::Ptr current_frame_=nullptr;
    Map::Ptr map_=nullptr;
    
    std::thread viewer_thread_;
    bool viewer_running_=true;
    
    std::unordered_map<unsigned long , Frame::Ptr> active_keyframes_;
    std::unordered_map<unsigned long , Mappoint::Ptr> active_landmarks_;
    bool map_updated_=false;
    
    std::mutex viewer_data_mutex_;
};
#endif // VIEWER_H
